/*
 * (C) 2014-2015 Alibaba Group Holding Limited
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 *
 *     __  ___   ______        ___       __          ___
 *    |  |/  /  /  __  \      /   \     |  |        /   \
 *    |  '  /  |  |  |  |    /  ^  \    |  |       /  ^  \
 *    |    <   |  |  |  |   /  /_\  \   |  |      /  /_\  \
 *    |  .  \  |  `--'  |  /  _____  \  |  `----./  _____  \
 *    |__|\__\  \______/  /__/     \__\ |_______/__/     \__\
 *
 *
 *
 */
//
// Created by KOALA TEAM on 12/19/14.
//

#ifndef KLRuntime_h
#define KLRuntime_h

#import <objc/runtime.h>
#import <objc/objc.h>


//define

#define Hooked_Orignal_Method(name)        _kl_orignal_##name
#define Hooked_Method(name)                _kl_hooked_##name
#define Hooked_Orignal_Selector(name)      @selector(Hooked_Orignal_Method(name))
#define Hooked_Selector(name)              @selector(Hooked_Method(name))


#define Add_Method_To_Class(_class,_selName) do{    \
            Method add_method = class_getInstanceMethod([self class], @selector(_selName)); \
            IMP    add_imp = method_getImplementation(add_method);      \
            class_addMethod(_class, @selector(_selName), add_imp, method_getTypeEncoding(add_method));    \
        }while(0)


#define HOOK_INST_METHOD(_class,_orgSelName,_hookedSelName)  do{ \
            if(NO&&class_getInstanceMethod([self class], Hooked_Orignal_Selector(_orgSelName))){ return;} \
            Method org_method = class_getInstanceMethod([self class], @selector(_orgSelName));  \
            Method rep_method = class_getInstanceMethod([self class], @selector(_hookedSelName));  \
            IMP    org_imp = method_getImplementation(org_method);      \
            class_addMethod([self class], Hooked_Orignal_Selector(_orgSelName), org_imp, method_getTypeEncoding(org_method)); \
            IMP    rep_imp = method_getImplementation(rep_method); \
            class_replaceMethod([self class], @selector(_orgSelName), rep_imp, method_getTypeEncoding(org_method));   \
        }while(0)


#define Set_Instance_Var(_obj,_name,_value) objc_setAssociatedObject(_obj,"_append_"#_name,_value,OBJC_ASSOCIATION_RETAIN_NONATOMIC)
#define Get_Instance_Var(_obj,_name)        objc_getAssociatedObject(_obj,"_append_"#_name)

#endif //KLRuntime_h
